<?php
	//created by mamat, July 18, 2007
	class Comment {
		function getNewComment($limit) {
			$mysql = new Mysql();
			$mysql->connect();
			$query = "select * from pagecomment order by commentId desc limit $limit";
			if($mysql->execute($query)) {
				return $mysql->getDataSet();
			} else {
				return false;
			}
				
			$mysql->closeConnection();
		}
		
		function getComment($pageDetailId) {
			$mysql = new Mysql();
			$mysql->connect();
			$query = "select * from pagecomment where pageDetailId = $pageDetailId order by commentId desc";
			if($mysql->execute($query)) {
				return $mysql->getDataSet();
			} else {
				return false;
			}
				
			$mysql->closeConnection();
		}
		
		function getCommentRows($pageDetailId) {
			$mysql = new Mysql();
			$mysql->connect();
			$query = " select count(commentId) from pagecomment where pageDetailId = $pageDetailId";
			
			if($mysql->execute($query)) {
				$countComment = $mysql->getDataSet();
				if (empty($countComment[0][0])) {
					$countComm = 0;
				} else {
					$countComm = $countComment[0][0];
				}
				
				return $countComm; 
			} else {
				return false;
			}
				
			$mysql->closeConnection();
		}
		
		function addComment($name, $email, $website, $comment, $pageDetailId) {
			//cek email
			if($this->cekEmail($email)) {			
				$mysql = new Mysql();
				$mysql->connect();
				
				$name = htmlentities($name);
				$website = htmlentities($website);
				$comment = htmlentities($comment);
				
				$query = "insert into pagecomment (name, email, website, comment, pageDetailId,date) values ('$name','$email','$website','$comment',$pageDetailId,CURRENT_TIMESTAMP())";
				if($mysql->execute($query)) {
					return true;
				} else {
					return false;
				}
					
				$mysql->closeConnection();
				
			} else {
				return false;
			}
		}
		
		//function for cek email
		function cekEmail($email){
			if (!eregi( "^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$", $email)) {
				return false;
			} else {
				return true;
			}
		}
	}

?>